/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tetris;

import java.awt.*;

/**
 *
 * @author CJ
 */
public class T_Block extends Block{

    public T_Block(int x, int y, int rate, char dir, int l, int w)
    {
        super(x, y, rate, dir, l, w);
        color = Color.darkGray;
    }

    public T_Block()
    {
        super();
        color = Color.darkGray;
    }

    /*
     * N = O
     *    OOO
     *
     * E = O
     *     OO
     *     O
     *
     * S = OOO
     *      O
     *
     * W = O
     *    OO
     *     O
     * */

    public void draw(Graphics g, Board b)
    {
        if(!stopped)
            shadowDraw(g, b);

        g.setColor(color);

        if(dir == 'N')
        {
            g.fillRect(x-10, y+10, 30, 10);
            g.fillRect(x, y, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x, y+10, 10, 10);
            g.drawRect(x + 10, y + 10, 10, 10);
            g.drawRect(x - 10, y + 10, 10, 10);
        }
        else if(dir == 'E')
        {
            g.fillRect(x, y, 10, 30);
            g.fillRect(x+10, y+10, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x, y+10, 10, 10);
            g.drawRect(x, y+20, 10, 10);
            g.drawRect(x+10, y+10, 10, 10);
        }
        else if(dir == 'S')
        {
            g.fillRect(x, y, 30, 10);
            g.fillRect(x+10, y+10, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x+10, y, 10, 10);
            g.drawRect(x+20, y, 10, 10);
            g.drawRect(x + 10, y+10, 10, 10);
        }
        else if(dir == 'W')
        {
            g.fillRect(x, y, 10, 30);
            g.fillRect(x - 10, y + 10, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x, y+10, 10, 10);
            g.drawRect(x, y+20, 10, 10);
            g.drawRect(x-10, y+10, 10, 10);
        }
    }

    public void makearea()
    {
        if(dir == 'N')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 2, x/10},
                {y/10 + 2, x/10 + 1},
                {y/10 + 2, x/10 - 1}};
        }
        else if(dir == 'E')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 2, x/10},
                {y/10 + 3, x/10},
                {y/10 + 2, x/10 + 1}};
        }
        else if(dir == 'S')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 1, x/10 + 1},
                {y/10 + 1, x/10 + 2},
                {y/10 + 2, x/10 + 1}};
        }
        else if(dir == 'W')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 2, x/10},
                {y/10 + 3, x/10},
                {y/10 + 2, x/10 - 1}};
        }

}

    public void rotate()
    {
        super.rotate();
        if(dir == 'S' && x + 30 > WIDTH)
            x -= 10;
        else if(dir == 'W' && x - 10 < 0)
            x += 10;
        else if(dir == 'N' && x + 30 > WIDTH)
            x -= 10;
        makearea();

    }

    public void setX(int x, Board b)
    {
        if((dir == 'N' && (x + 20 > WIDTH || x - 10 < 0))
                ||(dir == 'E' && (x + 20 > WIDTH || x < 0))
                ||(dir == 'S' && (x + 30 > WIDTH || x < 0))
                ||(dir == 'W' && (x + 10 > WIDTH || x - 10 < 0)))
            return;

        if(x > this.x)
        {
            //User wants to move to the right;
            if(!b.move(new T_Block(this.x + 10, y - 10, (int)rate, dir, LENGTH, WIDTH)))
                return;
        }
        else
        {
            //User wants to move to the left
            if(!b.move(new T_Block(this.x - 10, y - 10, (int)rate, dir, LENGTH, WIDTH)))
                return;
        }

        this.x = x;
        makearea();
    }

    public boolean canMoveY(Board b)
    {
        if((dir == 'N' && y + 20 >= LENGTH)
                || (dir == 'E' && y + 30 >= LENGTH)
                || (dir == 'W' && y + 30 >= LENGTH)
                || (dir == 'S' && y + 20 >= LENGTH)
                || !b.move(this))
        {
            b.put(this);
            return false;
        }
        return true;
    }

    public void shadowDraw(Graphics g, Board b)
    {
        T_Block theBlock = new T_Block(x, y, (int)rate, dir, LENGTH, WIDTH);

        while(((dir == 'N' && theBlock.getY() + 20 < LENGTH)
                || (dir == 'E' && theBlock.getY() + 30 < LENGTH)
                || (dir == 'W' && theBlock.getY() + 30 < LENGTH)
                || (dir == 'S' && theBlock.getY() + 20 < LENGTH))
                && b.move(theBlock))

        {
            theBlock.setY(theBlock.getY() + 10);
        }

        int y = theBlock.getY();

        g.setColor(Color.black);

        switch(dir)
        {
            case 'N':
                g.drawPolygon(new int[]{x, x + 10, x + 10, x + 20, x + 20, x - 10, x -10, x},
                        new int[]{y, y, y + 10, y + 10, y + 20, y + 20, y + 10, y + 10},
                        8);
                break;
            case 'E':
                g.drawPolygon(new int[]{x, x + 10, x + 10, x + 20, x + 20, x + 10, x + 10, x},
                        new int[]{y, y, y + 10, y + 10, y + 20, y + 20, y + 30, y + 30},
                        8);
                break;
            case 'S':
                g.drawPolygon(new int[]{x, x + 30, x + 30, x + 20, x + 20, x + 10, x + 10, x},
                        new int[]{y, y, y + 10, y + 10, y + 20, y + 20, y + 10, y + 10},
                        8);
                break;
            case 'W':
                g.drawPolygon(new int[]{x, x + 10, x + 10, x, x, x - 10, x - 10, x},
                        new int[]{y, y, y + 30, y + 30, y + 20, y + 20, y +10, y + 10},
                        8);
                break;
        }
        /*
         * N = O
         *    OOO
         *
         * E = O
         *     OO
         *     O
         *
         * S = OOO
         *      O
         *
         * W = O
         *    OO
         *     O
         * */
    }

}
